// pages/samplebinding/samplebinding.ts
import api from '../../utils/api';
import utils from "../../utils/utils"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1 as number,//1：新样本，0：已有样本 
    boundSampleList: [] as BoundSampleModel[],//已绑定列表
    page: 1,//页码
    pageSize: 10,//每页的数据
    isLoading: false as boolean,//是否正在加载
    code: '' as string,//样本编号
    totalCount: 0 as number,// 总共多少数据
    showForm: false as boolean,//是否展示表单,
    major:false as boolean,// 是否为大客户
    add:false as boolean,//是否过来添加
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {
    console.log(options)
    // 手动添加跳转过来的
    if(options.type == "add") {
      this.setData({
        add: true
      })
      return
    }
    try {
      this.setData({
        major: JSON.parse(options.major)
      })
    } catch (error) {
      this.setData({
        major: false
      })
      console.error("major为非boolean类型")
    } finally {
      this.getBindList();
    }
  },
  // 切换绑定新样本/已绑定样本
  changeType(e: any): void {
    this.setData({
      type: e.currentTarget.dataset.type,
    })
  },
  // 子组件绑定完成过来
  changeChildType(e: any): void {
    this.setData({
      type: e.detail.type,
      boundSampleList: [],//将之前的数据清空
      page: 1,
      totalCount: 0
    })
    this.getBindList()
  },
  // 批量绑定
  batchBind(): void {
    wx.navigateTo({
      url: "/pages/batchbind/batchbind"
    })
  },
  // 获取已绑定列表
  getBindList(): void {
    if (this.data.isLoading) {
      return
    }
    this.setData({
      isLoading: true
    })
    const params = {
      page: this.data.page,
      pageSize: this.data.pageSize
    }
    api.sampleBind.getBoundSampleList(params).then(res => {
      if (res.data.code == "000") {
        if (res.data.data && res.data.data.list) {
          const newData = res.data.data.list || [];
          const formattedNewData = newData.map(item => ({
            ...item,
            time: utils.timestampToDateTime(item.time! as number)
          }));

          this.setData({
            boundSampleList: [...this.data.boundSampleList, ...formattedNewData],
            page: this.data.page + 1,
            isLoading: false,
            totalCount: res.data.data.totalCount
          })
        }
      } else {
        this.setData({
          isLoading: false
        })
        wx.showToast({
          title: res.data.message as string,
          icon: "error",
          duration: 2000
        })
        console.error(res.data.message)
      }
    })
  },
  // 输入编号
  handleCode(e: any): void {
    this.setData({
      code: e.detail.value
    })
    if(this.data.showForm) {
      return
    }
    this.checkSampleCode(e.detail.value)
  },
  // 检查样本编号
  checkSampleCode(code: string): void {
    api.sampleBind.checkSampleCode({ code }).then(res => {
      if (res.data.code == "000") {
        this.setData({
          showForm:true
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon:"error"
        })
      }
    })
  },
  // 扫描二维码
  scanCode(): void {
    wx.scanCode({
      success: (res) => {
        console.log(res);
        this.setData({
          code:res.result
        })
        this.checkSampleCode(res.result)
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.type == 1) {
      return
    }
    if (this.data.totalCount == this.data.boundSampleList.length) {
      return
    }
    this.getBindList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})